import { personalChannel } from '@prisma/client';
import { BaseService } from '@/base/base.service';
import { Injectable } from '@nestjs/common';
import { LockChannelDto } from './dto/lock-channel-dto';
import { ConfigService } from '@nestjs/config';
const dayjs = require('dayjs')
const customParseFormat = require('dayjs/plugin/customParseFormat')
const isBetween = require('dayjs/plugin/isBetween')
dayjs.extend(isBetween)
dayjs.extend(customParseFormat)

@Injectable()
export class WxPersonalChannelService {

  constructor(private baseService: BaseService, private configService: ConfigService) {}

  async findNew() {
    return ((await this.baseService.personalChannel.findMany()).reverse()).slice(0, 4)
  }

  /**
   * 锁定车源
   */
  async lockChannel(dto: LockChannelDto) {
    const channel = await this.baseService.personalChannel.findUnique({
      where: {
        id: dto.id
      }
    })
    channel.isLock = true
    channel.lockWxUser = dto.userId
    let start = dayjs().format('YYYY-MM-DD').toString()
    let end = dayjs(start, 'YYYY-MM-DD').add(dto.lockDay, 'day').format('YYYY-MM-DD')
    channel.startLockDate = String(start)
    channel.endLockDate = String(end)
    await this.baseService.personalChannel.update({
      where: {
        id: channel.id
      },
      data: channel
    })
    const user = await this.baseService.wxUser.findUnique({
      where: {
        id: dto.userId
      }
    })
    const updteUser = await this.baseService.wxUser.update({
      where: {
        id: user.id
      },
      data: {
        coin: user.coin - dto.useCoin
      }
    })
    return updteUser
  }

  /**
   * 检查锁定时间是否过期
   * @param id 
   */
  async checkChannelLockExpire(id: string) {
    const channel = await this.baseService.personalChannel.findUnique({
      where: {
        id
      }
    })
    //检查是否锁定中 false未过期 true已过期
    if(channel.isLock) {
      //锁定中就判定时间
      const res = dayjs().isBetween(dayjs(channel.startLockDate, 'YYYY-MM-DD'), dayjs(channel.endLockDate, 'YYYY-MM-DD'))
      console.log(res);
      if(!res) {
        await this.baseService.personalChannel.update({
          where: {
            id
          },
          data: {
            isLock: false,
            startLockDate: null,
            endLockDate: null
          }
        })
        return true
      } else {
        return false
      }
    } else {
      return false
    }
  }

  async checkOuted(id: string) {
    const channel = await this.baseService.personalChannel.findUnique({
      where: {
        id
      }
    })
    const key = this.configService.get('CONFIG_KEY')
    const wxAppConfig = this.baseService.wxAppConfig.findUnique({
      where: {
        configKey: key
      }
    })
    //检查是否开启了限制
    if((await wxAppConfig).maxBuyCountLock) {
      //检查是否超过限制
      if(channel.buyCount >= (await wxAppConfig).maxBuyCount) {
        await this.baseService.personalChannel.update({
          where: {
            id
          },
          data: {
            outed: true
          }
        })
        return true
      } else {
        return false
      }
    } else {
      return false
    }
  }
}
